﻿/* ===============================================================================  
 * Copyright (c) 2010 Acid Frameworks
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * =============================================================================== */

using System;
using System.Collections.Generic;
using Acid.Math;

namespace Acid.Rendering
{
    /// <summary>
    /// A Transform is the primary means of positioning 2D and 3D objects in a Scene. It is referenced by a ViewInfo Object.
    /// </summary>
    public class Transform : IPackObject
    {
        private static int Count { get; set; }

        /// <summary>
        /// The Local Matrix for this Transform
        /// </summary>
        public Matrix          Local    { get; set; }
        /// <summary>
        /// The Parent Node of this Transform (Required to compute the World Matrix)
        /// </summary>
        public Transform       Parent   { get; set; }
        /// <summary>
        /// This Transforms Child Transforms
        /// </summary>
        public List<Transform> Transforms { get; set; }
        /// <summary>
        /// The Shapes attached to this Transform
        /// </summary>
        public List<Shape>     Shapes   { get; set; }

        public Transform()
        {
            this.ID           = "transform_" + Transform.Count.ToString(); Transform.Count ++;
            this.Local        = Matrix.Identity;
            this.Parent       = null;
            this.Transforms     = new List<Transform>();
            this.Shapes       = new List<Shape>();
        }

        #region Properties
    
        /// <summary>
        /// Computes the World Matrix of this Transform (note, traverses backward up the tree using this transforms Parent Nodes)
        /// </summary>
        public Matrix          World
        {
            get
            {
                Queue<Transform> queue = new Queue<Transform>();

                Transform transform = this;

                do {
                    queue.Enqueue(transform);
                
                    transform = transform.Parent;

                }while(transform != null);

                Matrix matrix = Matrix.Identity;

                while (queue.Count != 0)
                {
                    Transform top = queue.Dequeue();

                    matrix *= top.Local;
                }

                return matrix;
            }
        }
        
        #endregion

        #region IGraphObject Members

        /// <summary>
        /// This Transforms ID
        /// </summary>
        public string ID { get; set; }
        /// <summary>
        /// This Transforms Name
        /// </summary>
        public string Name { get; set; }

        #endregion
    }
}
